package com.uairobot.bill.asr.server.agent.controler;

import com.alibaba.fastjson.JSONObject;
import com.jfinal.core.Controller;
import com.jfinal.kit.Kv;
import com.jfinal.upload.UploadFile;
import com.litong.utils.baidu.asr.BaiduASRUtil;
import com.litong.utils.ibot.robot.AskRequest;
import com.litong.utils.ibot.robot.AskResponse;
import com.litong.utils.ibot.robot.RobotHttpClient;
import com.uairobot.bill.asr.server.agent.utils.PropKitUtil;

import lombok.extern.slf4j.Slf4j;

/**
 * @author bill robot
 * @date 2020年2月3日_下午12:51:59 
 * @version 1.0 
 * @desc
 */
@Slf4j
public class ApiSemanticsController extends Controller {

  public void index() {
    UploadFile uploadFile = getFile();
    // asr
    org.json.JSONObject asrResult = BaiduASRUtil.asr(uploadFile.getFile().getAbsolutePath(), "pcm", 16000, null);
    log.info("baidu asr result:"+asrResult.toString());
    String question = asrResult.getJSONArray("result").getString(0);

    Kv kv = getKv();
    String userId = kv.getStr("userId");
    String sessionId = kv.getStr("sessionId");
    // 进行nlp
    String robotURL = PropKitUtil.get("robot.url");
    AskRequest askRequest = new AskRequest();
    askRequest.setUserId(userId);
    askRequest.setSessionId(sessionId);
    askRequest.setFormat("json");
    askRequest.setQuestion(question);
    askRequest.setPlatform("tel");
    AskResponse ask = RobotHttpClient.ask(robotURL, askRequest);

    //替换空行
    String content = ask.getContent().replace("\r\n\r\n", ",");
    content = content.replace("\r\n", ",");
    
    //替换标点符号
    content= content.replace(" ", "");
    content= content.replace(".", ",");
    
    //替换[link]
    content = content.replace("[link]", "");
    content = content.replace("[/link]", "");
    
    //替换内容
    content=content.replace("点击相应问题回复答案", "");
    log.info(content);
    
    JSONObject data = new JSONObject();
    data.put("text",content);
    JSONObject retval = new JSONObject();
    retval.put("errno", 0);
    retval.put("data", data);
    renderJson(retval);
  }

}
